package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.Appointment;
import com.ruoyi.business.domain.StatementItem;
import com.ruoyi.business.mapper.AppointmentMapper;
import com.ruoyi.business.service.IAppointmentService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.StatementMapper;
import com.ruoyi.business.domain.Statement;
import com.ruoyi.business.service.IStatementService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;

import javax.sql.rowset.serial.SerialException;

/**
 * 结算单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-25
 */
@Service
@Transactional
public class StatementServiceImpl implements IStatementService {
    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 查询结算单
     * 
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public Statement selectStatementById(Long id)
    {
        return statementMapper.selectStatementById(id);
    }

    /**
     * 查询结算单列表
     * 
     * @param statement 结算单
     * @return 结算单
     */
    @Override
    public List<Statement> selectStatementList(Statement statement) {
        return statementMapper.selectStatementList(statement);
    }

    /**
     * 新增结算单
     * 
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int insertStatement(Statement statement)
    {
        statement.setCreateTime(DateUtils.getNowDate());
        return statementMapper.insertStatement(statement);
    }

    /**
     * 修改结算单
     * 
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int updateStatement(Statement statement)
    {
        return statementMapper.updateStatement(statement);
    }

    /**
     * 批量删除结算单
     * 
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementByIds(String ids) {
        return statementMapper.deleteStatementByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除结算单信息
     * 
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementById(Long id) {
        Statement statement = statementMapper.selectStatementById(id);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new ServiceException("已经进行消费, 不可进行删除");
        }
        return statementMapper.deleteStatementById(id);
    }

    @Override
    public void updateAmount(Long id, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal itemPrice) {
        statementMapper.updateAmount(id,totalAmount,totalQuantity,itemPrice);
    }

    //新增结算单服务
    @Override
    public int addSave(Statement statement) {
        Statement newStatement = new Statement();
        BeanUtils.copyProperties(statement,newStatement);
        newStatement.setStatus(Statement.STATUS_CONSUME);
        newStatement.setPayTime(null);
        newStatement.setCreateTime(new Date());
        return statementMapper.insertStatement(newStatement);
    }

    @Override
    public int editSave(Statement statement) {
        Statement currentStatement = statementMapper.selectStatementById(statement.getId());
        if (Statement.STATUS_PAID.equals(currentStatement.getStatus())){
            throw new ServiceException("该结算单已经完成消费");
        }
        Statement newStatement = new Statement();
        BeanUtils.copyProperties(statement,newStatement);
        newStatement.setStatus(Statement.STATUS_CONSUME);
        newStatement.setServiceType(statement.getServiceType());

        newStatement.setPayTime(null);
        return statementMapper.updateStatement(newStatement);
    }

    @Override
    public Long generateStatement(Long appId) {
        Appointment appointment = appointmentService.selectAppointmentById(appId);
        if (!(Appointment.STATUS_ARRIVAL.equals(appointment.getStatus()) ||
                Appointment.STATUS_CREATE.equals(appointment.getStatus()))){
            throw new ServiceException("非法操作");
        }
        //查看是否存在结算单
        Statement currendStatement = statementMapper.getByAppoiontmentId(appId);
        if (currendStatement == null) {
            //进行结算单生成
            Statement statement = new Statement();
            BeanUtils.copyProperties(appointment,statement);
            statement.setStatus(Statement.STATUS_CONSUME);
            statement.setCreateTime(new Date());
            statement.setServiceType(Long.valueOf(appointment.getServiceType().toString()));
            statement.setAppointmentId(appId);
            statementMapper.insertStatement(statement);
            currendStatement = statement;
        }
        //对预约服务状态进行修改
        appointment.setStatus(Appointment.STATUS_CREATE);
        appointmentMapper.updateAppointment(appointment);
        return currendStatement.getId();
    }
    /**
     * 支付结算单
     * @param statementId 结算单id
     */
    @Override
    public void pay(Long statementId) {
        Statement statement = statementMapper.selectStatementById(statementId);
        if (statement == null){
            throw new ServiceException("该结算单不存在");
        }
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new ServiceException("已经支付过该订单");
        }
        statementMapper.updateById(statementId,Statement.STATUS_PAID);
    }
}